﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class FormMain : Form
    {
        private static Random _rand = new Random();
        private const int ColumnCount = 10;

        public FormMain()
        {
            InitializeComponent();
        }

        private void ToggleButtons(bool enabled = true)
        {
            this.buttonCreateFile.Enabled = this.buttonReadEntire.Enabled = this.buttonReadLineByLine.Enabled = enabled;
        }

        private void buttonCreateFile_Click(object sender, EventArgs e)
        {
            ToggleButtons(false);
            using (var outStream = File.CreateText("testfile.txt"))
            {
                for (int i = 0; i < ColumnCount; i++)
                {
                    if (i > 0) outStream.Write("\t");
                    outStream.Write("Column_" + i);
                }
                outStream.WriteLine();

                int x, lcnt = int.Parse(textBoxLineCount.Text);
                for (int i = 0; i < lcnt; i++)
                {
                    for (int j = 0; j < ColumnCount; j++)
                    {
                        if (j > 0) outStream.Write("\t");

                        switch (j)
                        {
                            case 0:
                                outStream.Write(_rand.Next(0, 100));
                                break;
                            case 2:
                                x = _rand.Next(0, 3);
                                if (x == 0) outStream.Write("");
                                else outStream.Write((x == 1) ? bool.TrueString : bool.FalseString);
                                break;
                            case 4:
                                outStream.Write(_rand.Next(0, 1000) * _rand.NextDouble());
                                break;
                            case 6:
                                outStream.Write(DateTime.Now.AddHours(-1 * _rand.Next(0, 1000000)));
                                break;
                            default:
                                x = _rand.Next(0, 100);
                                for (int k = 0; k < x; k++)
                                {
                                    if (_rand.Next(0, 15) == 0) outStream.Write(" ");
                                    outStream.Write((char)_rand.Next(65, 91));
                                }
                                break;
                        }
                    }
                    outStream.WriteLine();
                }
            }
            ToggleButtons(true);
        }

        private void BindRecord(string[] data)
        {
            var idx = this.dataGridView1.Rows.Add();
            for (int i = 0; i < data.Length; i++)
                this.dataGridView1.Rows[idx].Cells[i].Value = data[i];

            if ((idx % 10) == 0)
            {
                Application.DoEvents();
            }
        }

        private void ResetAndBindHeaders(string[] data)
        {
            this.dataGridView1.Columns.Clear();
            this.dataGridView1.Rows.Clear();

            for (int i = 0; i < data.Length; i++)
                this.dataGridView1.Columns.Add(data[i], data[i]);
        }

        private void buttonReadEntire_Click(object sender, EventArgs e)
        {
            if (File.Exists("titles_vdb.txt"))
            {
                ToggleButtons(false);
                var start = DateTime.Now;
                var text = File.ReadAllText("titles_vdb.txt");

                bool firstLine = true;
                foreach (string line in text.Split(new string[] { "\r\n" }, StringSplitOptions.None))
                {
                    string[] data = line.Split('\t');
                    if (firstLine)
                    {
                        // this is the headers
                        if (checkBoxPopulateGrid.Checked)
                        {
                            ResetAndBindHeaders(data);
                        }
                        firstLine = false;
                    }
                    else
                    {
                        // this is the data
                        if (checkBoxPopulateGrid.Checked)
                        {
                            BindRecord(data);
                        }
                    }
                }

                var end = DateTime.Now;
                this.labelMemoryEntire.Text = string.Format("{0:n}", System.Diagnostics.Process.GetCurrentProcess().WorkingSet64);
                this.labelTimeEntire.Text = (end - start).ToString();
                ToggleButtons(true);
            }
        }
        
        private void buttonReadLineByLine_Click(object sender, EventArgs e)
        {
            if (File.Exists("titles_vdb.txt"))
            {
                ToggleButtons(false);
                var start = DateTime.Now;
                bool firstLine = true;
                using (var reader = new StreamReader("titles_vdb.txt"))
                {
                    var line = reader.ReadLine();
                    while (line != null)
                    {
                        string[] data = line.Split('\t');
                        if (firstLine)
                        {
                            // this is the headers
                            if (checkBoxPopulateGrid.Checked)
                            {
                                ResetAndBindHeaders(data);
                            }
                            firstLine = false;
                        }
                        else
                        {
                            // this is the data
                            if (checkBoxPopulateGrid.Checked)
                            {
                                BindRecord(data);
                            }
                        }
                        line = reader.ReadLine();
                    }
                }

                var end = DateTime.Now;
                this.labelMemoryLineByLine.Text = string.Format("{0:n}", System.Diagnostics.Process.GetCurrentProcess().WorkingSet64);
                this.labelTimeLineByLine.Text = (end - start).ToString();
                ToggleButtons(true);
            }
        }
    }
}
